<template>
    <el-row class="login-page">
        <el-col :span="12" class="bg"></el-col>
        <el-col :span="6" :offset="3" class="form">
            <!-- 注册表单 -->
            <el-form ref="form" size="large" autocomplete="off" :model="formData" :rules="rules">
                <el-form-item>
                    <h1>注册</h1>
                </el-form-item>
                <el-form-item prop="userName">
                    <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="formData.userName"></el-input>
                </el-form-item>
                <el-form-item prop="userPwd">
                    <el-input :prefix-icon="Lock" type="password" placeholder="请输入密码"
                        v-model="formData.userPwd"></el-input>
                </el-form-item>
                <el-form-item prop="rePassword">
                    <el-input :prefix-icon="Lock" type="password" placeholder="请输入再次密码"
                        v-model="formData.rePassword"></el-input>
                </el-form-item>
                <!-- 注册按钮 -->
                <el-form-item>
                    <el-button class="button" type="primary" auto-insert-space @click="registerUser">
                        注册
                    </el-button>
                </el-form-item>
                <el-form-item class="flex">
                    <el-link type="info" :underline="false" @click="clearRegisterData()">
                        ← 返回
                    </el-link>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
</template>



<script setup>
import FormComponent from '@/components/form/FormComponent.vue';
import { register } from '@/api/base'
import { ElMessage } from "element-plus";

let router = useRouter();
const alternatorRef = ref(null)
const form = ref(null)
const rules = reactive({
    userName: [
        { required: true, message: '请填写用户名', trigger: 'blur' },
        // { min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
    userPwd: [
        { required: true, message: '请填写密码', trigger: 'blur' },
    ]
});

const formData = reactive({
    userName: '',
    userPwd: '',
    rePassword: ''
})
const registerUser = () => {
    form.value.validate((valid) => {
        if (valid) {
            register(formData)
                .then(async (res) => {
                    if (res.code != "0000") {
                        ElMessage({
                            showClose: true,
                            message: res.msg,
                            type: "error",
                        });
                        form.value.resetFields();
                    } else {
                        ElMessage({
                            showClose: true,
                            message: "注册成功,请返回登录页进行登录",
                            type: "success",
                        });
                        form.value.resetFields();
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        } else {
            console.log('error submit!')
            return false
        }
    })
}
const clearRegisterData = () => {
    router.push("/login");
}
const onSubmitSuccess = (res) => {
    // 表单提交成功的回调函数
}
</script>

<style lang="scss" scoped>
/* 样式 */
.login-page {
    height: 100vh;
    background-color: #fff;

    .bg {
        background: url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
            url('@/assets/login_bg.jpg') no-repeat center / cover;
        border-radius: 0 20px 20px 0;
    }

    .form {
        display: flex;
        flex-direction: column;
        justify-content: center;
        user-select: none;

        .title {
            margin: 0 auto;
        }

        .button {
            width: 100%;
        }

        .flex {
            width: 100%;
            display: flex;
            justify-content: space-between;
        }
    }
}
</style>